/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FindReplace.java
 *
 * Created on 13.Haz.2009, 13:14:38
 */

package com.meraklisina.latexeditor;

import java.awt.event.KeyListener;
import java.text.NumberFormat;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.text.MaskFormatter;

import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;

/**
 *
 * @author okula
 */
public class GotoLine extends javax.swing.JPanel implements KeyListener {
	private JTextPane textEditor;
	private JDialog dialog;
	private JTabbedPane tab_Editors;
	/** Creates new form GotoLine */
	public GotoLine(JDialog jd, JTabbedPane editors) {
		this.dialog=jd;
		this.tab_Editors=editors;

		this.setFocusable(true);
		this.addKeyListener(this);

		initComponents();

		
		TxtFind.addKeyListener(this);
		GtLine.addKeyListener(this);
		BtnCancel.addKeyListener(this);

	}

	/** Returns active JTextPane object
	 */
	public JTextPane getTextEditor()
	{
		return new TextEditors().GetJTextPaneAt(this.tab_Editors, this.tab_Editors.getSelectedIndex());
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {


		GtLine = new javax.swing.JButton();
		BtnCancel = new javax.swing.JButton();
		try
		{
	//	MaskFormatter mf = new MaskFormatter("########");
		
		TxtFind = new javax.swing.JFormattedTextField(NumberFormat.getNumberInstance());
		}
		catch(Exception ex)
		{}
		jLabel1 = new javax.swing.JLabel();


		addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				formKeyPressed(evt);
			}
		});

		GtLine.setText("Go to Line");
		GtLine.setToolTipText("Go to Line");
		GtLine.setName("Go to Line"); // NOI18N
		GtLine.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				GtLineActionPerformed(evt);
			}
		});



		BtnCancel.setText("Cancel");
		BtnCancel.setToolTipText("Cancel");
		BtnCancel.setName("BtnCancel"); // NOI18N
		BtnCancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				BtnCancelActionPerformed(evt);
			}
		});

		TxtFind.setName("TxtFind"); // NOI18N

		
		jLabel1.setText("Line:");
		jLabel1.setName("jLabel1"); // NOI18N







		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jLabel1))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(TxtFind, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
														.addComponent(BtnCancel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(GtLine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
														.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(GtLine)
								.addComponent(TxtFind, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel1))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(layout.createSequentialGroup()
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(BtnCancel)))
																.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * Finds the position of the line number entered
	 * @return if the position is found 
	 *         
	 */
	private boolean find()
	{
		String b = TxtFind.getText().trim();
		int lineNo = 0;
		NumberFormat format =
		    NumberFormat.getIntegerInstance( );
		try
		{
		    Number number = format.parse( b );
		    lineNo = number.intValue( );
		}
		catch ( Exception e )
		{
		   
		}

		int indexIn  = getTextEditor().getText().indexOf("\n");
		int index   = getTextEditor().getText().indexOf("\n")+1;
		String txt = getTextEditor().getText();
		int count =1;
		int curIndex =0;
		if(lineNo < 2)
		{
			getTextEditor().setCaretPosition(0);
		}
		else
		{
			while(curIndex != -1 && count < (lineNo -1))
			{
				String cc= getTextEditor().getText().substring(index);
				curIndex = cc.indexOf("\n");
				if(curIndex != -1)
				{
					index += (curIndex +1);
					
				}
				
				
				count++;
			}
			if(curIndex != -1)
			{
				index = index - (count);
				getTextEditor().setCaretPosition(index);
				getTextEditor().setSelectionStart(index);
			}
			else
			{
				getTextEditor().setCaretPosition(getTextEditor().getText().length()-count-1);
				getTextEditor().setSelectionStart(getTextEditor().getText().length()-count-1);	
			}

			
		}

		this.dialog.setVisible(false);
		return true;
	}


	private void GtLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GtLineActionPerformed
		// TODO add your handling code here:
		find();
	}//GEN-LAST:event_GtLineActionPerformed

	private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
		// TODO add your handling code here:
		this.dialog.setVisible(false);
	}//GEN-LAST:event_BtnCancelActionPerformed



	public void keyPressed(java.awt.event.KeyEvent evt)
	{
		if(evt.getKeyCode()==evt.VK_ESCAPE)
			this.dialog.setVisible(false);
	}
	public void keyTyped(java.awt.event.KeyEvent ke){}
	public void keyReleased(java.awt.event.KeyEvent ke){}

	private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
		// TODO add your handling code here:
	}//GEN-LAST:event_formKeyPressed




	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton BtnCancel;
	private javax.swing.JButton GtLine;
	private javax.swing.JFormattedTextField TxtFind;
	private javax.swing.JLabel jLabel1;
	// End of variables declaration//GEN-END:variables

}
